<template>
  <div>
    <div>
      <p>商品清单如下:</p>
      <div v-for="(obj, index) in arr" :key="index">
        {{ obj.shopName }} -- {{ obj.price }}元/份
      </div>
      <p>请选择购买数量:</p>
      <ganfan
        v-for="(item, index) in arr"
        :key="index + ' '"
        :shopName="item.shopName"
        :count="item.count"
        :ind="index"
        @jia="jiayi"
        @jian="jianyi"
      ></ganfan>
    </div>
    <div>
      <p>总价为:{{ allPrice }}</p>
    </div>
  </div>
</template>

<script>
import ganfan from "./components/干饭.vue";
export default {
  data() {
    return {
      arr: [
        {
          shopName: "可比克薯片",
          price: 5.5,
          count: 0,
        },
        {
          shopName: "草莓酱",
          price: 3.5,
          count: 0,
        },
        {
          shopName: "红烧肉",
          price: 55,
          count: 0,
        },
        {
          shopName: "方便面",
          price: 12,
          count: 0,
        },
      ],
    };
  },
  components: {
    ganfan,
  },
  methods: {
    jiayi(ind) {
      this.arr[ind].count++;
    },
    jianyi(ind) {
      this.arr[ind].count--;
    },
  },
  computed: {
    allPrice() {
      return this.arr.reduce((sum, obj) => (sum += obj.count * obj.price), 0);
    },
  },
};
</script>

<style>
</style>